package com.lepu.app.fun.tool.szqx.bean;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.app.utils.DBHelper;

public class BeanToolGrow {
	private static final String TABLE_NAME = "ToolGrow";
	
	public int id;
	public String uid; 
    public int type; 
    public long time;
    public String weight;
    public String height;
    public String head;


    //添加
    public static boolean insert(BeanToolGrow bean) {
        boolean flag = false;

        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
        if (dbHelp == null)
            return flag;

        ContentValues values = new ContentValues();
        values.put("uid", bean.uid);
        values.put("type", bean.type);
        values.put("time", bean.time);
        values.put("weight", bean.weight);
        values.put("height", bean.height);
        values.put("head", bean.head);
        flag = dbHelp.insert(TABLE_NAME, null, values);

        return flag;
    }
    
    //获取所有数据
    public static ArrayList<BeanToolGrow> getDataByUid(String uid) {
    	ArrayList<BeanToolGrow> provinces = new ArrayList<BeanToolGrow>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "type", "time", "weight","height","head"}, 
					"uid=?",new String[] {uid}, null, null,"time asc");

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    //获取所有数据
    public static ArrayList<BeanToolGrow> getDataByUidDesc(String uid) {
    	ArrayList<BeanToolGrow> provinces = new ArrayList<BeanToolGrow>();
		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
		if (dbHelp == null)
			return provinces;

		SQLiteDatabase dbSqlite = null;
		try {
			dbSqlite = dbHelp.getWritableDatabase();

			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "type", "time", "weight","height","head"}, 
					"uid=?",new String[] {uid}, null, null,"time desc");

			cursorParse(provinces, queryResult);
			dbSqlite.close();
		} catch (Exception e) {
		}

		return provinces;
    }
    
    //获取时间段数据
    public static ArrayList<BeanToolGrow> getDataByDate(String uid, long fromMill, long toMill) {
    	ArrayList<BeanToolGrow> provinces = new ArrayList<BeanToolGrow>();
    	DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
    	if (dbHelp == null)
    		return provinces;
    	
    	SQLiteDatabase dbSqlite = null;
    	try {
    		dbSqlite = dbHelp.getWritableDatabase();
    		
    		Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "type", "time", "weight","height","head"}, 
    				"uid=? and time>=? and time<?",new String[] {uid,String.valueOf(fromMill), String.valueOf(toMill)}, null, null,"time desc");
    		
    		cursorParse(provinces, queryResult);
    		dbSqlite.close();
    	} catch (Exception e) {
    	}
    	
    	return provinces;
    }
    
    //获取所有数据
//    public static ArrayList<BeanToolGrow> getDataByDone(String uid, int done) {
//    	ArrayList<BeanToolGrow> provinces = new ArrayList<BeanToolGrow>();
//		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
//		if (dbHelp == null)
//			return provinces;
//
//		SQLiteDatabase dbSqlite = null;
//		try {
//			dbSqlite = dbHelp.getWritableDatabase();
//
//			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "name", "number","done"}, 
//					"uid=? and done=?",new String[] {uid,String.valueOf(done)}, null, null,null);
//
//			cursorParse(provinces, queryResult);
//			dbSqlite.close();
//		} catch (Exception e) {
//		}
//
//		return provinces;
//    }
    
    // 修改数据
//    public static boolean update(BeanToolGrow bean) {
//
//        boolean flag = false;
//
//        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
//        if (dbHelp == null)
//            return flag;
//
//        ContentValues values = new ContentValues();
//        
//        values.put("uid", bean.uid);
//        values.put("name", bean.name);
//        values.put("number", bean.number);
//        values.put("done", bean.done);
//
//        flag = dbHelp.update(TABLE_NAME, values, "uid=? and name=?", new String[]{bean.uid, bean.name});
//
//        return flag;
//    }
    
    
	//是否存在
//	public static boolean isExist(String name) {
//		ArrayList<BeanToolGrow> provinces = new ArrayList<BeanToolGrow>();
//		DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
//		if (dbHelp == null)
//			return false;
//
//		SQLiteDatabase dbSqlite = null;
//		try {
//			dbSqlite = dbHelp.getWritableDatabase();
//
//			Cursor queryResult = dbSqlite.query(TABLE_NAME, new String[] {"id","uid", "name", "number","done"}, 
//					"name=?", new String[] { name },null, null, null);
//			cursorParse(provinces, queryResult);
//			dbSqlite.close();
//		} catch (Exception e) {
//		}
//
//		if (provinces.size() > 0) {
//			return true;
//		} else {
//			return false;
//		}
//	}
	
	//删除某一条记录
//    public static boolean deleteItem(String uid, String name) {
//        boolean flag = false;
//
//        DBHelper dbHelp = DBHelper.getInstance(DBHelper.DATABASE_NAME_TOOL);
//        if (dbHelp == null)
//            return flag;
//
//        flag = dbHelp.delete(TABLE_NAME, "uid=? and name=?",new String[]{uid, name});
//
//        return flag;
//    }
	
	private static void cursorParse(ArrayList<BeanToolGrow> msgArray,Cursor cursor) {
		while (cursor.moveToNext()) {
			BeanToolGrow beanTarget = new BeanToolGrow();
			beanTarget.id = (cursor.getInt(cursor.getColumnIndex("id")));
			beanTarget.uid = (cursor.getString(cursor.getColumnIndex("uid")));
			beanTarget.type = (cursor.getInt(cursor.getColumnIndex("type")));
			beanTarget.time = (cursor.getLong(cursor.getColumnIndex("time")));
			beanTarget.weight = (cursor.getString(cursor.getColumnIndex("weight")));
			beanTarget.height = (cursor.getString(cursor.getColumnIndex("height")));
			beanTarget.head = (cursor.getString(cursor.getColumnIndex("head")));
			msgArray.add(beanTarget);
		}
	}
	
}
